﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using MicroCenter.Models;
using MicroCenter.Services;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MicroCenter.Controllers
{
    [Auth]
    [Route("api/[controller]")]
    [ApiController]
    public class GuardianController : ControllerBase
    {
        public readonly GuardianService guardianService;

        public readonly FileService fileService;

        public readonly LogService logService;
        /// <summary>
        /// 获取服务和状态
        /// </summary>
        /// <returns></returns>
        [Get]
        public ServiceStatusModel[] GetServices() => guardianService.GetAllServicesWithStatus();

        /// <summary>
        /// 设置自启动状态
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="isAutoRun"></param>
        [Get]
        public void SetAutoRun(string serviceName, bool isAutoRun) => guardianService.SetAutoRun(serviceName, isAutoRun);
        /// <summary>
        /// 设置全部服务自启动状态
        /// </summary>
        /// <param name="isAutoRun"></param>
        [Get]
        public void SetAllAutoRun(bool isAutoRun) => guardianService.SetAllAutoRun(isAutoRun);
        /// <summary>
        /// 创建一个服务
        /// </summary>
        /// <param name="serviceName"></param>
        [Get]
        public void Create(string serviceName)
        {
            guardianService.CreateService(serviceName);
            fileService.CreateVersion(serviceName);
        }

        /// <summary>
        /// 删除一个服务
        /// </summary>
        /// <param name="serviceName"></param>
        [Get]
        public void Delete(string serviceName)
        {
            guardianService.DeleteService(serviceName, false);
            fileService.DeleteDirectory(serviceName, null, null);
        }

        /// <summary>
        /// 运行一个服务
        /// </summary>
        /// <param name="serviceName"></param>
        [Get]
        public void Run(string serviceName) => guardianService.Run(serviceName, 5000);

        /// <summary>
        /// 运行服务从某个版本
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="version"></param>
        [Get]
        public void RunFromVersion(string serviceName, string version)
        {
            fileService.CheckVersion(serviceName, version);
            guardianService.TryStopService(serviceName);
            fileService.CopyToRun(serviceName, version);
            fileService.CopyConfigToRun(serviceName);
            guardianService.Empower(serviceName);
            guardianService.Run(serviceName, 5000);
        }
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName"></param>
        [Get]
        public void Kill(string serviceName) => guardianService.Kill(serviceName, 2000);
        /// <summary>
        /// 查看服务日志列表
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        [Get]
        public List<FileModel> LogList(string serviceName)
        {
            return logService.LookLogs(serviceName);
        }

        /// <summary>
        /// 查看日志详情
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [Get]
        public string LogDetail(string serviceName, string name)
        {
            return logService.Read(serviceName, name);
        }

    }
}
